<template>
  <div class="my">
    <!-- 未登录头部 -->
    <div class="header no-login" v-if="!userToken">
      <div class="toLogin" @click="$router.push('/login')">
        <img src="~@/assets/mobile.png" alt="" />
        <span>登录&nbsp;/&nbsp;注册</span>
      </div>
    </div>
    <!-- 已登录头部 -->
    <div class="header al-login" v-if="userToken">
      <!-- 上部分,头像和编辑资料 -->
      <div class="baseInfo">
        <div class="avatar">
          <van-image class="baseImg" :src="userInfo.photo" fit="cover" round />
          <span>{{ userInfo.name }}</span>
        </div>
        <van-button round class="editInfo" size="small" to="/user/profile">编辑资料</van-button>
      </div>
      <!-- 下部分,总览信息情况 -->
      <div class="simpleInfo">
        <div v-for="(item, i) in infoArr" :key="i" class="infoItem">
          <i>{{ item.count }}</i
          ><span>{{ item.name }}</span>
        </div>
      </div>
    </div>

    <!-- 宫格导航 -->
    <van-grid :column-num="2" clickable class="grid-nav">
      <van-grid-item class="nav-item">
        <i slot="icon" class="iconfont iconshoucang"></i>
        <span slot="text">收藏</span>
      </van-grid-item>
      <van-grid-item class="nav-item">
        <i slot="icon" class="iconfont iconlishi"></i>
        <span slot="text">历史</span>
      </van-grid-item>
    </van-grid>

    <van-cell title="消息通知" is-link></van-cell>
    <van-cell title="小智同学" is-link></van-cell>
    <van-cell
      title="退出登录"
      class="loginOut"
      v-if="userToken"
      @click="loginOut"
      clickable
    ></van-cell>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserOwnInfo } from '@/api/user'
export default {
  name: 'My',
  data() {
    return {
      userInfo: {}
    }
  },
  computed: {
    ...mapState(['userToken']),
    // 个人信息数组
    infoArr() {
      const arr = [
        { name: '头条', count: this.userInfo.art_count },
        { name: '关注', count: this.userInfo.follow_count },
        { name: '粉丝', count: this.userInfo.fans_count },
        { name: '获赞', count: this.userInfo.like_count }
      ]
      return arr
    }
  },
  methods: {
    loginOut() {
      // 询问用户
      this.$dialog
        .confirm({
          title: '是否退出'
        })
        .then(() => {
          // on confirm
          // 清除token,将null更新到vuex和本地
          this.$store.commit('setUserToken', null)
        })
        .catch(() => {
          // on cancel
          this.$toast.fail('已取消退出')
        })
    }
  },
  async created() {
    if (this.userToken) {
      try {
        const res = await getUserOwnInfo()
        this.userInfo = res.data.data
      } catch (error) {
        return this.$toast.fail('获取用户信息失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.header {
  background-image: url('~@/assets/banner.png');
  background-size: cover;
  height: 361px;
}

// 未登录头部样式
.no-login {
  display: flex;
  justify-content: center;
  align-items: center;
  .toLogin {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    img {
      width: 134px;
      height: 134px;
    }
    span {
      margin-top: 15px;
      font-size: 28px;
      color: #fff;
    }
  }
}
// 已登录头部样式
.al-login {
  box-sizing: border-box;
  padding: 76px 32px 0;
  // 上
  .baseInfo {
    // background-color: pink;
    height: 135px;
    margin-bottom: 22px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .avatar {
      display: flex;
      align-items: center;
      .baseImg {
        box-sizing: border-box;
        width: 135px;
        height: 135px;
        border: 2px solid #fff;
      }
      span {
        font-size: 30px;
        color: #fff;
        margin-left: 23px;
      }
    }
    .editInfo {
      color: #666;
      font-size: 20px;
      height: 35px;
    }
  }
  .simpleInfo {
    height: 130px;
    // background-color: pink;
    display: flex;
    justify-content: space-around;
    .infoItem {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      color: #fff;
      i {
        font-style: normal;
        font-size: 36px;
      }
      span {
        font-size: 23px;
      }
    }
  }
}
// 宫格导航
.grid-nav {
  margin-bottom: 10px;
  .nav-item {
    height: 144px;
    i {
      font-size: 45px;
      &.iconshoucang {
        color: #eb5253;
      }
      &.iconlishi {
        color: #ff9d1d;
      }
    }
    span {
      font-size: 28px;
    }
  }
}
.loginOut {
  margin-top: 10px;
  text-align: center;
  line-height: 102px;
  padding: 0;
  color: #d86262;
  font-size: 30px;
}
</style>
